package twoD.IsoPfem.EmbeddedCrackModel;

import inf.jlinalg.SolveFailedException;
import inf.v3d.view.Viewer;
import twoD.IsoPfem.Constraint;
import twoD.IsoPfem.Force;
import twoD.IsoPfem.IGeometry;
import twoD.IsoPfem.ElasticStructure;
import twoD.IsoPfem.Mesh;
import twoD.IsoPfem.MeshLibrary;
import twoD.IsoPfem.Node;
import twoD.IsoPfem.RealTable;
import twoD.IsoPfem.Visualizer;
import twoD.IsoPfem.Control.ArcLengthController;
import twoD.IsoPfem.Control.DisplacementConstraintCondition;
import twoD.IsoPfem.Control.IConstraintCondition;
import twoD.IsoPfem.Control.LoadConstraintCondition;
import twoD.IsoPfem.Control.SphereConstraintCondition;
import twoD.IsoPfem.utils.NodeUtilities;

public class SlabSpecimenDemo2 {

	/**
	 * function to visualize the structure
	 * 
	 * @param struct
	 */
	public void Visualize(ElasticStructure struct) {
		Viewer view = new Viewer();
		Visualizer viz = new Visualizer(struct, view);
		viz.automaticScale();
		viz.drawConstraints();
		viz.drawForces();
		viz.drawEdgeLoads();
		viz.drawElements();
		view.setVisible(true);
	}

	/**
	 * function to draw structure with cracks
	 * 
	 * @param struct
	 */
	public void VisualizeWithCrack(CrackStructure struct) {
		Viewer view = new Viewer();
		CrackVisualizer viz = new CrackVisualizer(struct, view);
		viz.automaticScale();
		viz.drawConstraints();
		viz.drawForces();
		viz.drawEdgeLoads();
		viz.drawElements();
		viz.drawCrack();
		view.setVisible(true);
	}

	public void crackTest() {
		Mesh M = MeshLibrary
				.createTriangleMeshFromGiD("D:/Giang/vgu/zWork/simple_beam2.gid/mesh1.txt");

		double E = 30e9;
		double nu = 0.18;
		double Dnn = 0.01 * E;
		double Dss = 0.01 * E;

		RealTable realTable = new RealTable(E, nu, Dnn, Dss);
		realTable.setThickness(1);
		realTable.setTensileStrength(3.5e6);
		realTable.setFractureEnergy(140);

		CrackStructure cStruct = MeshLibrary.createEmbededCrackModel(realTable,
				M);

		for (IGeometry g : M.getGeometries()) {
			cStruct.addElement(new EmbeddedCrackCST(realTable, g));
		}

		Constraint c1 = new Constraint(false, false);
		Constraint c2 = new Constraint(true, false);

		NodeUtilities.findNode(cStruct, 600, 0).get(0).setConstraint(c1);
		NodeUtilities.findNode(cStruct, 1119, 0).get(0).setConstraint(c2);

		Force f = new Force(0, -1e7);
		final Node forceNode1 = NodeUtilities.findNode(cStruct, 722, 330)
				.get(0);
		forceNode1.setForce(f);
		final Node forceNode2 = NodeUtilities.findNode(cStruct, 203, 330)
				.get(0);
		forceNode2.setForce(f.multiply(0.13));

		IConstraintCondition cc1 = new LoadConstraintCondition(1, 0.1);
		IConstraintCondition cc2 = new DisplacementConstraintCondition(1e-3,
				5e-4, forceNode1.getDofNumbers()[1]);
		IConstraintCondition cc3 = new SphereConstraintCondition(1e-3, 1e-3,
				forceNode1.getDofNumbers()[1]);
		ArcLengthController C = new ArcLengthController(cStruct, cc2);
		// set 10 for load control/displacement control
		C.setNumberOfStep(50);
		try {
			C.Iterate();
		} catch (SolveFailedException e) {
			e.printStackTrace();
		}
//		C.listingState();

		VisualizeWithCrack(cStruct);
	}

	public static void main(String[] args) {

		new SlabSpecimenDemo2().crackTest();
	}

}
